package bobaini.com.topic_exchange;

import bobaini.com.util.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: Product
 * author: bob.ly
 * Version: 1.0.0
 * DateTime: 2021/07/27-15:31:00
 * Description:
 */
@Slf4j
public class Product {
    private static final String TOPIC_EXCHANGE = "topic.exchange";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC);
        Map<String, String> map = new HashMap<>();
        map.put("quick.orange.rabbit", "被队列 Q1Q2 接收到");
        map.put("lazy.orange.elephant", "被队列 Q1Q2 接收到");
        map.put("quick.orange.fox", "被队列 Q1 接收到");
        map.put("lazy.brown.fox", "被队列 Q2 接收到");
        map.put("lazy.pink.rabbit", "虽然满足两个绑定但只被队列 Q2 接收一次");
        map.put("quick.brown.fox", "不匹配任何绑定不会被任何队列接收到会被丢弃");
        map.put("quick.orange.male.rabbit", "是四个单词不匹配任何绑定会被丢弃");
        map.put("lazy.orange.male.rabbit", "是四个单词但匹配 Q2");

        for (Map.Entry<String, String> param : map.entrySet()) {
            String key = param.getKey();
            String message = param.getValue();
            log.info("当前路由key:{}, 当前消息体:{}", key, message);
            channel.basicPublish(TOPIC_EXCHANGE, key, null, message.getBytes());
        }
    }
}
